package top.wu2020.lazy.ws.chat.infrastructure.persistence;

import top.wu2020.lazy.ws.chat.infrastructure.entity.ContentDO;
import top.wu2020.lazy.ws.chat.infrastructure.converter.ContentConverter;
import top.wu2020.lazy.ws.chat.infrastructure.mapper.ContentMapper;
import top.wu2020.lazy.ws.chat.domain.model.content.ContentRepository;
import org.springframework.stereotype.Repository;
import java.util.stream.Collectors;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import top.wu2020.lazy.ws.chat.domain.model.content.Content;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import java.util.List;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
/**
 * describe content 
 *
 * @author Jia wei Wu
 * @date 2024/05/24 08:52 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence 
 **/
@Repository
public class ContentRepositoryImpl  implements  ContentRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增
     *
     * @param content 新增     
     * @return {@link Result<Content>} 新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Content> story(Content content) {
        ContentDO contentDO = ContentConverter.INSTANCE.fromContent(content);
        lazyLambdaStream.upsert(contentDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增
     *
     * @param contentList 批量新增     
     * @return {@link Result<List<Content>>} 新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<Content>> batchStory(List<Content> contentList) {
        List<ContentDO> contentDOList = contentList.stream().map(ContentConverter.INSTANCE::fromContent).collect(Collectors.toList());
        lazyLambdaStream.upsert(contentDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个
     *
     * @param content 查询单个     
     * @return {@link Result<Content>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Content> findOne(Content content) {
        ContentDO contentDO = ContentConverter.INSTANCE.fromContent(content);
        Content contentOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(contentDO), Content.class);
        return ResultFactory.successOf(contentOne);
    }

    /**
     * describe 查询多个
     *
     * @param content 查询多个     
     * @return {@link Result<List<Content>>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<Content>> findList(Content content) {
        ContentDO contentDO = ContentConverter.INSTANCE.fromContent(content);
        List<Content> contentList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(contentDO), Content.class);
        return ResultFactory.successOf(contentList);
    }

    /**
     * describe 分页查询多个
     *
     * @param size 当前页数
     * @param current 当前页
     * @param content 分页查询多个     
     * @return {@link Result<LazyPage<Content>>} 分页领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<LazyPage<Content>> findPage(int size,int current,Content content) {
        ContentDO contentDO = ContentConverter.INSTANCE.fromContent(content);
        LazyPage<Content> lazyPage = new LazyPage<>(current,size);
        LazyPage<Content> contentLazyPage = lazyLambdaStream.selectPage(LazyWrappers.lambdaWrapperBean(contentDO),lazyPage, Content.class);
        return ResultFactory.successOf(contentLazyPage);
    }

    /**
     * describe 删除
     *
     * @param content 删除     
     * @return {@link Result<Content>}      
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Content> remove(Content content) {
        ContentDO contentDO = ContentConverter.INSTANCE.fromContent(content);
        //  lazyLambdaStream.delete(LazyWrappers.lambdaWrapperBean(contentDO));
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在
     *
     * @param content 领域对象     
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Boolean> exists(Content content) {
        ContentDO contentDO = ContentConverter.INSTANCE.fromContent(content);
        Boolean exists=lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(contentDO));
        return ResultFactory.successOf(exists);
    }

}